// pages/fenXiao/yjpm/yjpm.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myRanking:1,
    tab1:['运动排行','魅力排行'],
    tab2:['日榜','周榜','月榜'],
    lj:0,
    rankList:[],
    flag:true, //我的名次显示状态
    gd:{
      bigText:'累计步数',
      notice: '排名为定时刷新(最多显示100名)',
      th:['排名','昵称','累计步数']
    },//一些固定的文本

    phNameType:'run',
    phTimeType:'day',
    flag2:true, //点赞按钮的状态
    fuserInfo:null
  },

  tab1(e){
    //console.log(e.detail.id);
    let id=e.detail.id;
    let bigText;
    let phNameType;
    let th;
    if(id==0){
      bigText ='累计步数';
      th = ['排名', '昵称', '累计步数'];
      phNameType='run'
    }
    else{
      bigText = '魅力值'
      th = ['排名', '昵称', '魅力值'];
      phNameType='meiLi'
    }

    this.setData({
      ['gd.bigText']:bigText,
      ['gd.th']:th,
      phNameType
    });
    //console.log(this.data.phNameType);
    this.getRunRank(this.data.phNameType, this.data.phTimeType);
  },

  tab2(e){
    let id = e.detail.id;
    let phTimeType;
    switch(id){
      case 0:
        phTimeType='day';
        break;
      case 1:
        phTimeType='week';
        break;
      case 2:
        phTimeType='month';
        break;
    }

    this.setData({
      phTimeType
    })
    //console.log(this.data.phTimeType);
    this.getRunRank(this.data.phNameType, this.data.phTimeType);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.cloud.callFunction({
      name:'register'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRunRank(this.data.phNameType, this.data.phTimeType);
  },

  /**
   * 获取排行列表
   */
  getRunRank(t1,t2){
    wx.showLoading({
      title: '正在加载',
    });
    wx.cloud.callFunction({
      name:'getPhb',
      data:{
        t1,
        t2
      }
    }).then(res=>{
      //console.log(res.result);
      let list=res.result.data;
      let myData=res.result.myData;
      if(t1=='run'){
      this.setData({
          myRanking:res.result.wdpm,
          lj: myData.hasOwnProperty('dayData') ? myData.dayData : (myData.hasOwnProperty('weekData') ? myData.weekData.ws : myData.monthData.ms),
          rankList:list
        });
      }
      else{
        this.setData({
          myRanking: res.result.wdpm,
          lj: myData.hasOwnProperty('dayRec') ? myData.dayRec : (myData.hasOwnProperty('weekRec') ? myData.weekRec : myData.monthRec),
          rankList:list
        })
      }
      wx.hideLoading();
    })
  },

  /**
   * 点赞
   */
  thump(e){
    if(!this.data.flag2){return;} //防止用户重复点赞
    wx.showLoading({});
    this.setData({
      flag2:false
    })
    //console.log(e.currentTarget.dataset);
    let openid = e.currentTarget.dataset.openid;
    let isup = e.currentTarget.dataset.isup
    let index = e.currentTarget.dataset.index
    wx.cloud.callFunction({
      name:'thump',
      data:{
        t1: this.data.phNameType,
        t2: this.data.phTimeType,
        op:openid
      }
    }).then(res=>{
      //console.log(res.result);
      if(res.result.code==0){
        let n=this.data.rankList[index].upNum;
        let key1 = 'rankList['+index+'].upNum';
        let key2 = 'rankList[' + index +'].curUp';
        this.setData({
          [key1]:n+(isup?-1:1),
          [key2]:!isup
        })
      }

      this.setData({
        flag2:true
      })
      wx.showToast({
        title: res.result.mes,
      })
    })
  },

  addFriend(e){
    const{openid,name,index}=e.currentTarget.dataset;
    //console.log(openid);
    let nameShow=name;
    if(name.length>5){
      nameShow=name.substr(0,5)+'...';
    }

    wx.showModal({
      title: '好友添加',
      content: '将'+nameShow+'加入好友列表',
      icon:'none',
      success:res=>{
        if(res.confirm){
          wx.cloud.callFunction({
            name:'friend',
            data:{
              act:'add',
              fopenid:openid,
              fname:nameShow
            }
          }).then(res=>{
            let code=res.result.code
            if(code==0||code==2){
              this.setData({
                ['rankList['+index + '].isFriend']:true
              })
            }
            wx.showToast({
              title: res.result.mes,
            })
          })
        }
      }
    })
  },

  sendFriInfo(e){
    let i=e.currentTarget.dataset.index;
    let fuserInfo=this.data.rankList[i].userData;
    this.setData({
      fuserInfo
    })
    this.selectComponent('#popgift1').showModel();
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  load:function(){
    this.setData({
      flag:false
    });
  }
})